<?php
/*                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                            O\ = /O
 *                        ____/`---'\____
 *                      .   ' \\| |// `.
 *                       / \\||| : |||// \
 *                     / _||||| -:- |||||- \
 *                       | | \\\ - /// | |
 *                     | \_| ''\---/'' | |
 *                      \ .-\__ `-` ___/-. /
 *                   ___`. .' /--.--\ `. . __
 *                ."" '< `.___\_<|>_/___.' >'"".
 *               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *                 \ \ `-. \_ __\ /__ _/ .-` / /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *
 *         .............................................
 *                  佛祖保佑             永无BUG
 *
 * ======================================================
 * @author: Ethan Lu <ethan.lu@qq.com>
 * 
 */

namespace frontend\models;

use common\models\User;
use yii\base\Model;

class RegistrationForm extends Model
{

    public $mobile;

    public $password;

    public $is_read;

    public $code;


    public function rules()
    {

        return [
            'mobileRequired' => ['mobile', 'required'],
            'mobileLength' => ['mobile', 'string', 'length' => 11],
            'mobileUnique' => [
                'mobile', 'unique', 'targetClass' => User::className(),
                'message' => '{attribute}{value}已被占用!'
            ],

            'codeRequired' => ['code', 'required'],
            'codeValidate' => ['code', 'frontend\components\sms\SmsValidator', 'targetField' => 'mobile'],

            'passwordRequired' => ['password', 'required'],
            'passwordLength' => ['password', 'string', 'min' => 6],

            'isReadRequired' => ['is_read', 'required', 'message' => '请接受服务条款'],
        ];

    }

    /** @inheritdoc */
    public function attributeLabels()
    {
        $attributeLabel['mobile']    = '手机号码';
        $attributeLabel['password'] = '密码';
        $attributeLabel['code'] = '短信验证码';
        return $attributeLabel;
    }

    /**
     * Registers a new user account.
     * @return bool
     */
    public function register()
    {
        if ($this->validate()) {
            $user = new User();
            $user->mobile = $this->mobile;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
        }

        return null;
    }

    public function formName()
    {
        return '';
    }
}